package com.xujiang.common_adapter.view;

import android.content.Context;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.ViewConfiguration;

/**
 *
 *  解决SwipeRefreshLayout下拉与左右滑动手势冲突
 * @author http://stackoverflow.com/questions/23989910/horizontalscrollview-inside-swiperefreshlayout
 * @author xujiang
 * @version 2017-07-04 14:50
 */
public class FixTouchSlopSwipeRefreshLayout extends SwipeRefreshLayout {

    private int   mTouchSlop;
    private float mPrevX;

    public FixTouchSlopSwipeRefreshLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {

        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                mPrevX = MotionEvent.obtain(ev).getX();
                break;

            case MotionEvent.ACTION_MOVE:
                final float eventX = ev.getX();
                float xDiff = Math.abs(eventX - mPrevX);

                if (xDiff > mTouchSlop) {
                    return false;
                }
        }
        return super.onInterceptTouchEvent(ev);
    }
}
